 
var editing  = false;
 
if (document.getElementById && document.createElement) {
	var butt = document.createElement('BUTTON');
	butt.setAttribute('name', 'butt');
	var buttext = document.createTextNode('Ready!');
	butt.appendChild(buttext);
	butt.onclick = saveEdit;
}

function catchIt(e) {
	if (editing) return;
	if (!document.getElementById || !document.createElement) return;
	if (!e) var obj = window.event.srcElement;
	else var obj = e.target;
	while (obj.nodeType != 1) {
		obj = obj.parentNode;
	}
	if (obj == null || obj.tagName == 'BUTTON' || obj.tagName == 'TEXTAREA' || obj.tagName == 'A') return;
	while (obj.nodeName != 'P' && obj.nodeName != 'HTML') {
		obj = obj.parentNode;
	}
	if (obj.nodeName == 'HTML')
		return;
	var x = obj.innerHTML;
	var z = obj.parentNode;
	if(z.id == "passTD")
	{
		var oldPass = document.createElement('div');
		oldPass.setAttribute('id', 'firstDivId');
		oldPass.innerHTML = "old password: " + document.getElementById("hiddenPass").value;
		var validatePassword = document.createElement('div');
		validatePassword.setAttribute('id', 'secondDivId');
		validatePassword.setAttribute('class', 'errorText');
		validatePassword.innerHTML = "new password and cofirmation";
		var newPass = document.createElement('input');
		var br = document.createElement('BR');
		var br2 = document.createElement('BR');
		var confirmNewPass = document.createElement('input');
		newPass.setAttribute('type', 'password');
		confirmNewPass.setAttribute('type', 'password');
		newPass.setAttribute('id', 'password');
		confirmNewPass.setAttribute('id', 'confirm');
		newPass.setAttribute('maxlength', '32');
		confirmNewPass.setAttribute('maxlength', '32');	
		newPass.setAttribute('onkeypress', 'return isPasswordKey(event);');
		confirmNewPass.setAttribute('onkeypress', 'return isPasswordKey(event);');
		newPass.setAttribute('onkeyup', 'checkPasswordStrength(this.value); checkConfirm();');
		confirmNewPass.setAttribute('onkeyup', 'checkConfirm();');
		newPass.setAttribute('value', '');
		confirmNewPass.setAttribute('value', '');
		z.insertBefore(oldPass,obj);
		z.insertBefore(br, obj);
		z.insertBefore(newPass,obj);
		z.insertBefore(br2, obj);
		z.insertBefore(confirmNewPass,obj);
		z.insertBefore(butt,obj);
		z.insertBefore(validatePassword,obj);
		z.removeChild(obj);
		newPass.focus();
		editing = true;
		
	}
	else
	{
		var y = document.createElement('input');
		y.setAttribute('type', 'text');
		y.setAttribute('maxlength', '32');
		if(z.id == "fullnameTD")
			y.setAttribute('onkeypress', 'return isNameKey(event);');
		else if(z.id == "phoneTD")
			y.setAttribute('onkeypress', 'return isNumberKey(event);');
		z.insertBefore(y,obj);
		z.insertBefore(butt,obj);
		z.removeChild(obj);
		y.value = x;
		if(z.id != "fullnameTD")
			if(y.value == "add?")
				y.value = "";
		y.focus();
		editing = true;
	}
	return false;
}
 
function saveEdit() {
	var area = document.getElementsByTagName('input')[0];
	var y = document.createElement('P');
	y.setAttribute('class', 'par3');
	var z = area.parentNode;
	var action = "";
	if(z.id == "passTD")
	{
		action = "password";
		var area2 = document.getElementsByTagName('input')[1];
		var div = document.getElementById("firstDivId");
		var br = document.getElementsByTagName('br')[0];
		var br2 = document.getElementsByTagName('br')[1];
		var validatePassword = document.getElementById("secondDivId");
		if(area.value == "")
			if(area2.value == "")
				{
					validatePassword.innerHTML = "no empty password";
					return false;
				}
		if(area.value != area2.value)
		{
			validatePassword.innerHTML = "passwords don't match";
			return false;
		}
		y.innerHTML = 'change password?';
		z.insertBefore(y,div);
		z.removeChild(div);
		z.removeChild(br);
		z.removeChild(area);
		z.removeChild(br2);
		z.removeChild(area2);
		z.removeChild(document.getElementsByTagName('button')[0]);
		z.removeChild(validatePassword);
		z.parentNode.childNodes[3].innerHTML = "";
		submitForm(action, area.value);
		editing = false;
		return false;
	}
	else if(z.id == "fullnameTD")			
		action = "name";
	else if(z.id == "phoneTD")
		action = "phone";
	else if(z.id == "addressTD")
		action = "address";
	y.innerHTML = area.value;
	if(z.id == "fullnameTD")
		if(area.value == "")
			return false;
	if(y.innerHTML == "")
		y.innerHTML = "add?";
	z.insertBefore(y,area);
	z.removeChild(area);
	z.removeChild(document.getElementsByTagName('button')[0]);
	submitForm(action, area.value);
	editing = false;
	return false;
}

function submitForm(action, value) {
	var editForm = document.getElementById("editForm");
	editForm.action.value = action;
	editForm.newValue.value = value;
	$(editForm).submit();
}

document.onclick = catchIt;
